import 'package:flutter/material.dart';
import 'package:my_salary/pages/about/about_page.dart';
import 'package:my_salary/pages/auth/forgot_password_page.dart';
import 'package:my_salary/pages/auth/register_page.dart';
import 'package:my_salary/pages/home/home_page.dart';
import 'package:my_salary/pages/settings/locale/language_settings_page.dart';
import 'package:my_salary/pages/settings/privacy/privacy_policy_page.dart';
import 'package:my_salary/pages/settings/profile/profile_page.dart';
import 'package:my_salary/pages/settings/security/bind_phone.dart';
import 'package:my_salary/pages/settings/security/change_password.dart';
import 'package:my_salary/pages/settings/security/devices.dart';
import 'package:my_salary/pages/settings/security/security_setting_page.dart';
import 'package:my_salary/pages/settings/settings_page.dart';
import 'package:my_salary/pages/settings/theme/theme_mode_page.dart';
import 'package:my_salary/pages/statistics/statistics_page.dart';
import 'package:my_salary/pages/timeline/timeline_page.dart';
import '../pages/auth/login_page.dart';
import '../pages/auth/logout_page.dart';
import '../pages/consumption_calendar/consumption_calendar_page.dart';
import '../pages/settings/security/signature_board.dart';
import '../pages/splash_page.dart';
import '../pages/todo/todo_page.dart';

enum PageTransitionType { slide, fade, scale, rotate }

///路由地址
class RoutePath {
  static const splash = '/splash';
  static const home = '/home';
  static const calendar = '/calendar';
  static const timeline = '/timeline';
  static const statistics = '/statistics';
  static const todo = '/todo';
  static const settings = '/settings';
  static const about = '/about';
  static const profile = '/profile';
  static const securitySettings = '/securitySettings';
  static const changePassword = '/changePassword';
  static const bindPhone = '/bindPhone';
  static const signature = '/signature';
  static const devices = '/devices';
  static const themeSettings = '/themeSettings';
  static const languageSettings = '/languageSettings';
  static const privacyPolicy = '/privacyPolicy';
  static const login = '/login';
  static const logout = '/logout';
  static const register = '/register';
  static const forgotPassword = '/forgotPassword';
}

///路由注册管理类
class Routes {
  /// 主路由生成器，统一动画风格
  static Route<dynamic> generateRoute(RouteSettings settings) {
    Widget? page;
    PageTransitionType transition = PageTransitionType.slide;

    switch (settings.name) {
      case RoutePath.splash:
        page = const SplashPage();
        transition = PageTransitionType.fade;
        break;
      case RoutePath.home:
        page = const HomePage();
        transition = PageTransitionType.fade;
        break;
      case RoutePath.calendar:
        page = const ShiftCalendarPage();
        transition = PageTransitionType.fade;
        break;
      case RoutePath.timeline:
        page = const TimelinePage();
        break;
      case RoutePath.statistics:
        page = const StatisticsPage();
        break;
      case RoutePath.todo:
        page = const TodoPage();
        break;
      case RoutePath.settings:
        page = const SettingsPage();
        break;
      case RoutePath.about:
        page = const AboutPage();
        break;
      case RoutePath.profile:
        page = const ProfilePage();
        break;
      case RoutePath.securitySettings:
        page = const SecuritySettingsPage();
        break;
      case RoutePath.changePassword:
        page = const ChangePasswordPage();
        break;
      case RoutePath.bindPhone:
        page = const BindPhonePage();
        break;
      case RoutePath.signature:
        page = const SignatureBoard();
        break;
      case RoutePath.devices:
        page = const DevicesPage();
        break;
      case RoutePath.themeSettings:
        page = const ThemeModePage();
        break;
      case RoutePath.languageSettings:
        page = const LanguageSettingsPage();
        break;
      case RoutePath.privacyPolicy:
        page = const PrivacyPolicyPage();
        break;
      case RoutePath.login:
        page = const LoginPage();
        transition = PageTransitionType.scale;
        break;
      case RoutePath.logout:
        page = const LogoutPage();
        break;
      case RoutePath.register:
        page = const RegisterPage();
        break;
      case RoutePath.forgotPassword:
        page = const ForgotPasswordPage();
        break;
    }

    if (page != null) {
      return animatedRoute(
        page,
        settings: settings,
        transition: transition,
      );
    }

    // 未定义路由 fallback
    return MaterialPageRoute(
      builder: (context) => Scaffold(
        body: Center(child: Text('No route defined for ${settings.name}')),
      ),
      settings: settings,
    );
  }

  /// 动画路由构造器
  static PageRouteBuilder animatedRoute(
    Widget page, {
    RouteSettings? settings,
    PageTransitionType transition = PageTransitionType.slide,
    Duration duration = const Duration(milliseconds: 300),
    Curve curve = Curves.ease,
    bool fullscreenDialog = false,
    bool maintainState = true,
  }) {
    return PageRouteBuilder(
      settings: settings,
      maintainState: maintainState,
      fullscreenDialog: fullscreenDialog,
      transitionDuration: duration,
      pageBuilder: (_, __, ___) => page,
      transitionsBuilder: (_, animation, __, child) {
        switch (transition) {
          case PageTransitionType.fade:
            return FadeTransition(opacity: animation, child: child);
          case PageTransitionType.scale:
            return ScaleTransition(scale: animation, child: child);
          case PageTransitionType.rotate:
            return RotationTransition(turns: animation, child: child);
          case PageTransitionType.slide:
          default:
            final tween = Tween(begin: const Offset(1.0, 0.0), end: Offset.zero)
                .chain(CurveTween(curve: curve));
            return SlideTransition(
                position: animation.drive(tween), child: child);
        }
      },
    );
  }
}
